package np13.ferry;

/**
 * Represents a place where one {@link Ferry} may be piered. This {@link Ferry}
 * may leave at any time. If there is no piered {@link Ferry}, any {@link Ferry}
 * may pier.
 */
public class Pier {

	/**
	 * The piered {@link Ferry}.
	 */
	private Ferry pieredFerry;
	private String location;

	/**
	 * Handles the arrival of an {@link Ferry}, by connecting it to the pier.
	 * 
	 * @param pieredFerry
	 *            The appearing Ferry.
	 */
	protected synchronized void arrive(Ferry pieredFerry) {
		assert (this.pieredFerry == null);
		this.pieredFerry = pieredFerry;
		notifyAll();
	}

	/**
	 * 
	 * @return Returns whereabouts of a pier. (Home Land or Funafitu).
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * Gets the {@link Ferry} which is currently anchored. Blocks until there is
	 * a Ferry available.
	 * 
	 * @return The piered Ferry.
	 * @throws InterruptedException
	 */
	public synchronized Ferry getPieredFerry() throws InterruptedException {
		while (pieredFerry == null) {
			wait();
		}
		return pieredFerry;
	}

	/**
	 * Remove the Ferry from the pier. This should be called by the
	 * {@link Ferry} when it leaves.
	 */
	public void leave() {
		pieredFerry = null;
	}

	/**
	 * Sets the whereabouts of a pier.
	 * 
	 * @param string
	 *            Localization of a pier.
	 */
	public void setLocation(String string) {
		location = string;
	}
}
